<?php
namespace app\admin\controller;
class Admin extends Common
{

    //用户管理
    public function index()
    {   
        if(request() -> isPost()){
            $key = input('post.key');
            $page = input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):10;
            $list = db('ucenter_members')
                 -> where('uid|username','like','%'.$key.'%')
                 ->order('uid desc')
                 -> paginate(array('list_rows'=>$pageSize,'page'=>$page))
                 ->toArray();
            if($list['data']){     
                foreach ($list['data'] as $key => $value) {
                    $list['data'][$key]['regdate'] = date('Y-m-d H:i:s',$value['regdate']);
                }  
            }             
            return json(['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1]);
        }
        return $this->fetch();
    }

    //用户个人信息
    public function info(){
        $uid = session('uid');
        $list = db('ucenter_members')->where('uid',$uid)->find();
        $this->assign('list',$list);
        return $this->fetch();
    }
    
    //添加用户
    public function add()
    {   
        if(request() ->isPost()){
            $data['username'] = trim(input('post.username'));
            $username = db('ucenter_members')->where($data)->find();
            if($username){
                return json(['code'=>0,'msg'=>'添加失败,用户名已经存在']);
            }
            $data['email'] = trim(input('post.email'));
            $email = db('ucenter_members')->where('email',$data['email'])->find();
            if($email){
                return json(['code'=>0,'msg'=>'添加失败,邮箱已经存在']);
            }
            $data['salt'] = random(6);
            $password = trim(input('post.password'));
            $data['password'] = md5(md5($password).$data['salt']);
            $data['cate'] = input('post.cate');
            $data['regip'] = request()->ip();
            $data['regdate'] = time();
            $members = db('ucenter_members')->insertGetId($data);
            if($members){
                return json(['code'=>1,'msg'=>'添加成功','url' => url('admin/index')]);
            }else{
                return json(['code'=>0,'msg'=>'添加失败']);
            }
        }else{    
           return $this->fetch();
        }
    }

    //编辑用户
    public function edit($uid='')
    {
        if(request() -> isPost()){
            $uid = input('post.uid');
            $username = trim(input('post.username'));
            $email = trim(input('post.email'));
            $cate = input('post.cate');
            $usernames = db('ucenter_members')->where('username',$username)->where('uid','<>',$uid)->find();
            if($usernames){
                return json(['code'=>0,'msg'=>'编辑失败,用户名已经存在']);
            }
            
            $emails = db('ucenter_members')->where('email',$email)->where('uid','<>',$uid)->find();
            if($emails){
                return json(['code'=>0,'msg'=>'编辑失败,邮箱已经存在']);
            }else{
                $update =db('ucenter_members')->where('uid', $uid)->update(['username' => $username,'email' => $email,'cate' => $cate]);
                if($update){
                    return json(['code'=>1,'msg'=>'编辑成功','url' => url('admin/index')]);
                }else{
                    return json(['code'=>0,'msg'=>'编辑失败']);
                }
            }
        }else{
            $list = db('ucenter_members')->where('uid',$uid)->find();
            $this->assign('list',$list);
            return $this->fetch();
        }
    }

    //编辑用户密码
    public function editpass($uid=''){
        if(request() -> isPost()){
            $uid = input('post.uid');
            $password = trim(input('post.password'));
            $pass = db('ucenter_members')->field('salt')->where('uid',$uid)->find();
            $data['password'] = md5(md5($password).$pass['salt']);
            $update =db('ucenter_members')->where('uid', $uid)->update($data);
            if($update){
                return json(['code'=>1,'msg'=>'编辑成功','url' => url('admin/index')]);
            }else{
                return json(['code'=>0,'msg'=>'编辑失败']);
            }
        }else{
            return $this->fetch();
        }
    }

    //删除单条记录(删除数据)
    public function del()
    {   
        $data['uid'] = input('param.uid');  
        if($data){
            $del = db('ucenter_members')->where($data)->delete();
            if($del){
                return json(['code'=>1,'msg'=>'删除成功']);
            }else{
                return json(['code'=>0,'msg'=>'删除失败']);
            }
        }
        
    }

    //删除单条记录(删除数据)
    public function delall()
    {   
        if(request() -> isPost()){
            $uids = $_POST['uids'];  
            foreach ($uids as $key => $value) {
                if($value == 1){
                    return json(['code'=>0,'msg'=>'admin管理员不能删除']);
                }
            }
            $all = db('ucenter_members')->delete($uids);
            if($all){
                return json(['code'=>1,'msg'=>'删除成功']);
            }else{
                return json(['code'=>0,'msg'=>'删除失败']);
            }
        }
    }
}